<?php

class RestController extends Zend_Controller_Action
{
    public function listFeedsAction()
    {
        $s = new FeedService();
        
        $this->view->feeds = $s->listFeeds();
    }

    public function createFeedAction()
    {
        $s = new FeedService();
        
        $data = array(
        	'name'=>$this->_getParam('name',null),
        	'title'=>$this->_getParam('title',null),
        	'description'=>$this->_getParam('description',null),
        );

        if($data['name']===null)
        {
            $this->view->httpCode = 403;
            $this->view->httpMessage = 'No feed name specified';
            $this->view->code = 403;
            $this->view->message = 'No feed name specified';
        }
        else
        {
            try
            {
                $s->addFeed($data['name'],$data);
                $this->view->code = 200;
                $this->view->message = "Feed created";
            }
            catch(Exception $e)
            {
                $this->view->httpCode = 500;
                $this->view->httpMessage = 'Server error';
                $this->view->code = 500;
                $this->view->message = $e->getMessage();
            }
        }
    }

    public function addEntryAction()
    {
    }

    public function deleteFeedAction()
    {
        $s = new FeedService();

        $name = $this->_getParam('name',null);

        if($name===null)
        {
            $this->view->httpCode = 404;
            $this->view->httpMessage = 'Not Found';
            $this->view->code = 404;
            $this->view->message = 'No feed name specified';
        }
        else
        {
            try
            {
                $s->deleteFeed($name);
                $this->view->code = 200;
                $this->view->message = "Feed deleted";
            }
            catch(Exception $e)
            {
                $this->view->httpCode = 500;
                $this->view->httpMessage = 'Server error';
                $this->view->code = 500;
                $this->view->message = $e->getMessage();
            }
        }
    }
    
    public function getFeedAction()
    {
        $s = new FeedService();
        
        try
        {
            $this->view->feed = $s->getFeedByName($this->_getParam('feed'));
        }
        catch(NotFoundException $e)
        {
            $this->render('notfound');
        }
    }

}
